home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / TransEdit.cpt / FakeAlert.c < prev    next >
Text File  |  1989-02-18  |  4KB  |  167 lines

  1. # include    <DialogMgr.h>
  2.  
  3.  
  4. # define    nil        (0L)
  5.  
  6.  
  7. typedef    int        Integer;    /* compiler 2-byte integer type */
  8. typedef    long    Longint;    /* compiler 4-byte integer type */
  9.  
  10.  
  11. /*
  12.     In-memory item list for dialog with four items:
  13.  
  14.     1    "^0^1^2^3" (static text)
  15.     2    Button 1
  16.     3    Button 2
  17.     4    Button 3
  18.  
  19.     The caller of FakeAlert passes the four strings that are to be
  20.     substituted into the first item, the number of buttons that
  21.     should be used, and the titles to put into each button.
  22.     A copy of the item list is hacked to use the right number of
  23.     buttons.
  24.  
  25.     Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  26.     this is modified from code they wrote.
  27. */
  28.  
  29.  
  30. static Integer    itemList [] =
  31. {
  32.     3,                    /* max number of items - 1 */
  33.  
  34. /*
  35.     statText item
  36. */
  37.     0, 0,                /* reserve a Longint for item handle */
  38.     10, 27, 61, 225,    /* display rectangle */
  39.     ((8+128) << 8) | 8,    /* 8 + 128 = statText (disabled), title 8 bytes long */
  40.     '^0', '^1',        /* ^0^1^2^3 */
  41.     '^2', '^3',
  42.  
  43. /*
  44.     first button
  45. */
  46.  
  47.     0, 0,                /* reserve a Longint for item handle */
  48.     104, 140, 124, 210,    /* display rectangle */
  49.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long*/
  50.  
  51. /*
  52.     second button
  53. */
  54.  
  55.     0, 0,                /* reserve a Longint for item handle */
  56.     104, 30, 124, 100,    /* display rectangle */
  57.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long */
  58.  
  59. /*
  60.     third button
  61. */
  62.  
  63.     0, 0,                /* reserve a Longint for item handle */
  64.     72, 30, 92, 100,    /* display rectangle */
  65.     (4 << 8) | 0        /* 4 = pushButton, title is 0 bytes long */
  66. };
  67.  
  68.  
  69. /*
  70.     Set dialog button title and draw bold outline if makeBold true.
  71.     This must be done after the window is shown or else the bold
  72.     outline won't show up (which is probably the wrong way to do it).
  73. */
  74.  
  75. static SetDControl (theDialog, itemNo, title, makeBold)
  76. DialogPtr    theDialog;
  77. Integer        itemNo;
  78. StringPtr    title;
  79. Boolean        makeBold;
  80. {
  81. Handle        itemHandle;
  82. Integer        itemType;
  83. Rect        itemRect;
  84. PenState    pState;
  85.  
  86.     GetDItem (theDialog, itemNo, &itemType, &itemHandle, &itemRect);
  87.     SetCTitle (itemHandle, title);
  88.     if (makeBold)
  89.     {
  90.         GetPenState (&pState);
  91.         PenNormal ();
  92.         PenSize (3, 3);
  93.         InsetRect (&itemRect, -4, -4);
  94.         FrameRoundRect (&itemRect, 16, 16);
  95.         SetPenState (&pState);
  96.     }
  97. }
  98.  
  99.  
  100. /*
  101.     Fake an alert, using an in-memory window and item list.
  102.     The message to be presented is constructed from the first
  103.     four arguments.  nButtons is the number of buttons to use,
  104.     defButton is the default button, the next three args are
  105.     the titles to put into the buttons.  The return value is
  106.     the button number (1..nButtons).  This must be interpreted
  107.     by the caller, since the buttons may be given arbitrary
  108.     titles.
  109.  
  110.     nButtons should be between 1 and 3, inclusive.
  111.     defButton should be between 1 and nButtons, inclusive.
  112. */
  113.  
  114.  
  115. FakeAlert (s1, s2, s3, s4, nButtons, defButton, t1, t2, t3)
  116. StringPtr    s1, s2, s3, s4;
  117. Integer        nButtons;
  118. Integer        defButton;
  119. StringPtr    t1, t2, t3;
  120. {
  121. GrafPtr        savePort;
  122. register DialogPtr    theDialog;
  123. register Handle        iListHandle;
  124. Rect        bounds;
  125. Integer        itemHit;
  126.  
  127.     InitCursor ();
  128.     GetPort (&savePort);
  129.     iListHandle = NewHandle (512L);
  130.     HLock (iListHandle);
  131.     BlockMove (&itemList, *iListHandle, 512L);
  132.     ((Integer *) *iListHandle)[0] = nButtons;    /* = number items - 1 */
  133.     SetRect (&bounds, 115, 80, 355, 220);
  134.     theDialog = NewDialog (nil, &bounds, "\p", false, dBoxProc, -1L,
  135.                             false, 0L, iListHandle);
  136.  
  137.     ParamText (s1, s2, s3, s4);        /* construct message */
  138.  
  139.     SetPort (theDialog);
  140.     ShowWindow (theDialog);
  141.  
  142.     switch (nButtons)                /* set button titles */
  143.     {
  144.         case 3:
  145.             SetDControl (theDialog, 4, t3, defButton == 3);
  146.             /* fall through... */
  147.         case 2:
  148.             SetDControl (theDialog, 3, t2, defButton == 2);
  149.             /* fall through... */
  150.         case 1:
  151.             SetDControl (theDialog, 2, t1, defButton == 1);
  152.     }
  153.  
  154. /*
  155.     ModalDialog returns 1 if return/enter hit, which, since
  156.     the statText item is first, can be unambiguously
  157.     interpreted as "choose default".
  158. */
  159.     ModalDialog (nil, &itemHit);
  160.     itemHit = (itemHit == 1 ? defButton : itemHit - 1);
  161.     HUnlock (iListHandle);
  162.     /*HPurge (iListHandle);*/
  163.     DisposDialog (theDialog);
  164.     SetPort (savePort);
  165.     return (itemHit);
  166. }
  167.